<?php
namespace App\Controller;

class CommentsController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.05.18
   * ps：获取评论状态
   * */
    public function getComment()
    {
        $content_type = $this->request->getData("content_type");
        $content_id = $this->request->getData("content_id");
        if(empty($content_type)){
            $this->G->error("content_type_error");
            return null;
        }
        if(!is_numeric($content_id)){
            $this->G->error("content_id_error");
            return null;
        }
        $comment = $this->Comments->find()
            ->where(['content_type'=>$content_type, 'content_id'=>$content_id])
            ->first();
        $this->G->success('Success', $comment);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.05.18
   * ps：删除评论状态
   * */
    public function deleteComment()
    {
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $comment = $this->Comments->findById($id)->first();
        if(empty($comment)){
            $this->G->error("comment_error");
            return null;
        }
        $this->Comments->deleteAll(['id'=>$id]);
        $this->loadComponent('CommentCache');
        $this->CommentCache->delete($comment['content_type'], $comment['content_id']);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.05.18
   * ps：新增评论状态
   * */
    public function addComment()
    {
        $content_type = $this->request->getData("content_type");
        $content_id = $this->request->getData("content_id");
        $status = $this->request->getData("status");
        $msgbox = $this->request->getData("msgbox");
        if(empty($content_type)){
            $this->G->error("content_type_error");
            return null;
        }
        if(!is_numeric($content_id)){
            $this->G->error("content_id_error");
            return null;
        }
        if(!in_array($status, ['show','hidden','verfiy','group'])){
            $this->G->error("status_error");
            return null;
        }
        if(!in_array($msgbox, ['show','hidden'])){
            $this->G->error("msgbox_error");
            return null;
        }
        $comment = $this->Comments->newEntity();
        $comment->content_type = $content_type;
        $comment->content_id = $content_id;
        $comment->status = $status;
        $comment->msgbox = $msgbox;
        $this->Comments->save($comment);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：设置指定评论状态
   * */
    public function setComment()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $status = $this->request->getData("status");
        $msgbox = $this->request->getData("msgbox");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!in_array($status, ['show','hidden','verfiy','group'])){
            $this->G->error("status_error");
            return null;
        }
        if(!in_array($msgbox, ['show','hidden'])){
            $this->G->error("msgbox_error");
            return null;
        }
        $comments = $this->Comments->findById($id)->first();
        $this->Comments->query()
            ->update()
            ->set(['status'=>$status,'msgbox'=>$msgbox])
            ->where(['id' => $id])
            ->execute();
        $this->loadComponent('CommentCache');
        $this->CommentCache->deleteCache($comments['content_type'], $comments['content_id']);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取所有的评论
   * */
    public function getComments()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $content_type = $this->request->getData("content_type");
        $content_id = $this->request->getData("content_id");
        $page = $this->request->getData("page");
        $where = [];
        if(!empty($content_type)){
            $where['content_type'] = $content_type;
        }
        if(!empty($content_id)){
            $where['content_id'] = $content_id;
        }
        $page = empty($page) ? 1 : $page;
        $comments = $this->Comments->find()
            ->where($where)
            ->order(['id desc'])
            ->limit($this->_page_num)
            ->page($page);
        $re = [];
        $count = $comments->count();
        $re['pageTotal'] = ceil($count/$this->_page_num);
        $comments = $comments->toArray();
        $re['list'] = [];
        if(!empty($comments)){
            foreach ($comments as $k=>$v){
                $re['list'][] = $v;
            }
        }
        $this->G->success("Success", $re);
    }

}
?>